package com.inspur.cloud.entity.dataspace.special;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

public class RangerPolicyItem {
    private static final Logger logger = LoggerFactory.getLogger(RangerPolicyItem.class);

    List<String> groups;
    List<String> users;
    List<String> access;
    String delegateAdmin;
    boolean bAdd;

    public RangerPolicyItem() {
        bAdd = true;
        groups = new ArrayList<String>();
        users = new ArrayList<String>();
        access = new ArrayList<String>();
        delegateAdmin = "false";
    }

    public JSONArray getGroupJson() {
        JSONArray groupArray = new JSONArray();
        for (String group : groups) {
            groupArray.add(group);
        }
        return groupArray;
    }

    public JSONArray getUserJson() {
        JSONArray userArray = new JSONArray();
        for (String user : users) {
            userArray.add(user);
        }
        return userArray;
    }

    public void addGroups(String group) {
        groups.add(group);
    }

    public void addUsers(String user) {
        users.add(user);
    }

    public void addAccess(String acc) {
        access.add(acc);
    }

    public JSONObject toJsonObj() {
        JSONObject item = new JSONObject();

        JSONArray accessArray = new JSONArray();
        for (String acc : access) {
            JSONObject accessObj = new JSONObject();
            accessObj.put("isAllowed", "true");
            accessObj.put("type", acc);
            accessArray.add(accessObj);
        }
        item.put("accesses", accessArray);

        JSONArray groupArray = new JSONArray();
        for (String group : groups) {
            groupArray.add(group);
        }
        item.put("groups", groupArray);

        JSONArray userArray = new JSONArray();
        for (String user : users) {
            userArray.add(user);
        }
        item.put("users", userArray);

        item.put("delegateAdmin", delegateAdmin);

        logger.info(item.toString());
        return item;
    }

    public boolean isbAdd() {
        return bAdd;
    }

    public void setbAdd(boolean bAdd) {
        this.bAdd = bAdd;
    }

    public String getDelegateAdmin() {
        return delegateAdmin;
    }

    public void setDelegateAdmin(String delegateAdmin) {
        this.delegateAdmin = delegateAdmin;
    }

    public List<String> getGroups() {
        return groups;
    }

    public void setGroups(List<String> groups) {
        this.groups = groups;
    }

    public List<String> getUsers() {
        return users;
    }

    public void setUsers(List<String> users) {
        this.users = users;
    }

    public List<String> getAccess() {
        return access;
    }

    public void setAccess(List<String> access) {
        this.access = access;
    }
}
